#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :jiaoyanyun.py
# @Time      :2023/11/20 
# @Author    :CL
# @email     :1037654919@qq.com
# 爬取教研云    反扒：'访问过于频繁，请稍后再试或联系在线客服

import requests
import json
from utils import mongo_manager
jiaoyanyun_question = mongo_manager('jiaoyanyun_question',db ='public_data')


headers = {
    "authority": "app-pub.jiaoyanyun.com",
    "accept": "application/json, text/plain, */*",
    "accept-language": "zh-CN,zh;q=0.9",
    "authorization": "8f96bc92-cee6-4123-9226-4df164fd7f88",
    "cache-control": "no-cache",
    "content-type": "application/json;charset=UTF-8",
    "origin": "https://xbresource.jiaoyanyun.com",
    "pragma": "no-cache",
    "referer": "https://xbresource.jiaoyanyun.com/",
    "sec-ch-ua": "\"Not.A/Brand\";v=\"8\", \"Chromium\";v=\"114\", \"Google Chrome\";v=\"114\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Linux\"",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-site",
    "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
}
def get_question(data):
    url = "https://app-pub.jiaoyanyun.com/xbresource-pub/v1/question/page"

    data = json.dumps(data, separators=(',', ':'))
    response = requests.post(url, headers=headers, data=data)

    # print(response.text)
    # print(response)
    if response.status_code == 200:
        return response.json()['data']['data']
data = {
            "subjectId": "11",  # 1 语文 2 数学 3 英语 4 物理 5 化学 6 生物  8 历史 9 地理 10 政治 11科学
            "gradeGroupId": "1",  # 1 小学 2 初中 3 高中
            "pageNo": 1,  # 页码
            "muti": "0",  # 多选题 1 开启

        }
print(get_question(data))

def main():

    for gradeGroupId in range(1, 4):
        for subjectId in range(1, 12):
            for page in range(1, 6):
                print(subjectId,gradeGroupId,page)
                q_data = {
                    "sortOrderList": [
                        {
                            "order": 1,
                            "name": "综合排序",
                            "sortName": "comprehensive_score",
                            "sortOrder": "desc",
                            "functionPointCode": "SORT_ZongHePaiXu"
                        }
                    ], # 排序
                    "subjectId": str(subjectId),  # 1 语文 2 数学 3 英语 4 物理 5 化学 6 生物  8 历史 9 地理 10 政治 11科学
                    "gradeGroupId": str(gradeGroupId),  # 1 小学 2 初中 3 高中
                    # "schoolYear": [
                    #     {
                    #         "name": "2023~2024",
                    #         "id": "20232024",
                    #         "code": "schoolYear"
                    #     }
                    # ],  # 学年
                    "pageNo": page,  # 页码
                    "muti": "0",  # 多选题 1 开启
                    # "qi": {
                    #     "schoolYearIdList": [
                    #         "20232024"
                    #     ]
                    # },  # 参数
                    # "subjectName": "语文",
                    # "gradeGroupName": "高中",
                    # "pageSize": 10,
                    # "labelType": "cataSystem"
                }

                try:
                    datas = get_question(q_data)

                    if datas:
                     for data in datas:

                        result={}
                        result['testId'] = data['testId']
                        result['queId'] =data['queId']
                        result['subjectId'] =data['subjectId']
                        result['subjectName'] =data['subjectName']
                        result['gradeGroupId'] =data['gradeGroupId']
                        result['gradeGroupName'] =data['gradeGroupName']
                        result['content'] =data['content']
                        result['createDate'] =data['createDate']
                        result['isAnalysis'] =data['isAnalysis']
                        result['isCheck'] =data['isCheck']
                        result['isDecidable'] =data['isDecidable']
                        result['isHaveAnalysis'] =data['isHaveAnalysis']
                        result['isNested'] =data['isNested']
                        result['isObjective'] =data['isObjective']
                        result['isTemporaryTable'] =data['isTemporaryTable']
                        result['isTraditional'] =data['isTraditional']
                        result['keyWord'] =data['keyWord']
                        result['logicQuesTypeId'] =data['logicQuesTypeId']
                        result['logicQuesTypeName'] =data['logicQuesTypeName']
                        result['quePreviewUrl'] =data['quePreviewUrl']
                        result['queSource'] = data['queSource']
                        result['writtenQuesTypeId'] =data['writtenQuesTypeId']
                        result['writtenQuesTypeName'] = data['writtenQuesTypeName']
                        result['_id'] = data['queId']
                        try:
                            jiaoyanyun_question.insertOne(result)
                        except Exception as e:
                            print(e)
                except Exception as e:
                    print(e)

if __name__ == '__main__':
    print()
    # main()


